import 'package:flutter/material.dart';

/// A collection of datetime operation
///
import "package:intl/intl.dart";

DateFormat defaultDateFormat = DateFormat("d/M/y H:m");

DateFormat yyyymmdd = DateFormat('y/M/d');

String fromTS(int timestamp, {String fallback = "--"}) {
  if (timestamp is int) {
    final dt = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return defaultDateFormat.format(dt);
  }
  return fallback;
}

DateTime fromYMD(String ymd) {
  return DateTime.parse(ymd);
}

// 转换系统默认的时间
String toDefaultDateString({String? date, String? time}) {
  if (date == null || time == null) {
    return '-';
  }
  DateTime fixturedate = DateTime.parse("${date} ${time}");
  return DateFormat("dd-MM-yyyy HH:mm").format(fixturedate);
}

extension CompareTimeOfDayAndConvertToTimeString on TimeOfDay {
  int toMinutes() {
    return hour * 60 + minute;
  }

  bool operator >(TimeOfDay other) {
    if (other is TimeOfDay) {
      return toMinutes() > other.toMinutes();
    }
    return false;
  }

  bool operator >=(TimeOfDay other) {
    if (other is TimeOfDay) {
      return toMinutes() >= other.toMinutes();
    }
    return false;
  }

  bool operator <(TimeOfDay other) {
    if (other is TimeOfDay) {
      return toMinutes() < other.toMinutes();
    }
    return false;
  }

  bool operator <=(TimeOfDay other) {
    if (other is TimeOfDay) {
      return toMinutes() <= other.toMinutes();
    }
    return false;
  }

  String toReadableString({bool seconds = false}) {
    return "${this.hour.toString().padLeft(2, "0")}:${this.minute.toString().padLeft(2, "0")}" /* + (seconds ? ":00" : "") */;
  }
}
